//
//  KBBaseViewController.swift
//  KimboWeiBoSwift
//
//  Created by kimbo on 15/12/16.
//  Copyright © 2015年 kimbo. All rights reserved.
//

import UIKit

class KBBaseViewController: UITableViewController {

    var userLogin = KBUserAccount.userLogin //是否登录成功
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //是否登录成功,显示不同的view
        userLogin ? super.loadView() : setupVistorView()
    }
    
    //提示登录view
    func setupVistorView() {
        
        view = vistorView
        
        //设置代理
        vistorView.delegate = self
        
        //根据控制器的不同显示不同信息
        if self is KBHomeViewController {
            
            vistorView.startRotationAnimation()
            
            //监听通知,解决回到后台在回到前台的跳动问题
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
            
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil)
        } else if self is KBMessageViewController {
            
            vistorView.setupInfo("visitordiscover_image_message", message: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知")
        } else if self is KBDiscoveryViewController {
            
            vistorView.setupInfo("visitordiscover_image_message", message: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
        } else if self is KBProfileViewController {
            
            vistorView.setupInfo("visitordiscover_image_profile", message: "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")
        }
        
        //设置导航栏左右item
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "registButtonDidClick")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "loginButtonDidClick")
    }
    
    //移除通知
    deinit {
        if self is KBHomeViewController {
            NSNotificationCenter.defaultCenter().removeObserver(self)
        }
    }
    
    // MARK: 应用回到前后台切换的监听方法
    func didEnterBackground() {
        vistorView.pauseAnimation()
    }
    
    func didBecomeActive() {
        vistorView.resumeAniation()
    }
    
    // MARK: 懒加载
    lazy var vistorView: KBVistorView = KBVistorView()

}


// MARK: 扩展,实现KBVistorViewDelegate协议
// extension 和 OC 中的 Category非常类似, 可以将协议的相关方法放到一起, 方便统一管理代码
extension KBBaseViewController: KBVistorViewDelegate {
    // MARK: 实现代理方法
    //点击注册按钮
    func registButtonDidClick() {
        print("\(__FUNCTION__)")
    }
    
    //点击登陆按钮
    func loginButtonDidClick() {
        
        //modal出OAuth控制器
        let oAuthVc = KBOAuthViewController()
        presentViewController(UINavigationController(rootViewController: oAuthVc), animated: true, completion: nil)
    }
}

